#include "threadPool.h"                                                                                                     
int func_puts(int netfd,char * file_name,numStack_t * stack,MYSQL * mysql,char * user_name)
{
    char VFAT_path[200]={0};
    func_pwd(VFAT_path,stack,mysql);
    strcat(VFAT_path,"/");
    strcat(VFAT_path,file_name);
    int strLength = strlen(VFAT_path); // 计算字符串的长度
    int point = 0; // 字符串位置标号，用于保存当前有效字符位置
    for (int i = 0; i < strLength; i++) {
        if (i!=0&& VFAT_path[i] == '/'&&VFAT_path[i-1]=='/') {
            continue; // 若当前字符为要删除的字符，则直接跳过继续进行循环
        } else {
            VFAT_path[point] = VFAT_path[i]; // 当前字符为非删除字符，则往前覆盖
            point++; // 有效字符标号位置递增
        }
    }
    VFAT_path[point] = '\0'; // 循环结束，最后一个字符为结束符
    printf("虚拟文件路径是%s\n",VFAT_path);
    char sql[300]={0};
    sprintf(sql,"select * from file_excel where path = '%s' and is_deleted = 0 and user_name = '%s' ;",VFAT_path,user_name);
    int ret=mysql_query(mysql,sql);
    MYSQL_RES * result = mysql_store_result(mysql);//得到查询结果
    if(ret!=0)
    {   
        printf("Error: %s\n", mysql_error(mysql));
        printf("数据库查找失败，用户gets的目标不存在\n");
        mysql_free_result(result);
        printf("发送bbbbb\n");
        send(netfd,"bbbbb",5,0);
        return -1;
    }
    //判断是否查询到的为空
    MYSQL_ROW row=mysql_fetch_row(result);
    if(row==NULL)
    {   
        mysql_free_result(result);
        printf("数据库查找到的是空行，用户gets的目标不存在\n");
        printf("发送bbbbb\n");  
        send(netfd,"bbbbb",5,0);
         return -1;
     }
     char hash[200]={0};
     strcat(hash,row[7]);//查到了那么就拿出哈希值
     mysql_free_result(result);
 
     char true_file_path[500]={0};
     strcat(true_file_path,PRE_PATH);
     strcat(true_file_path,"/");
     strcat(true_file_path,hash);
 
     if(access(true_file_path,F_OK)==-1)//文件不存在
     {
         printf("在服务器本地文件中，有用户请求文件hash为名字的不存在，用户gets的目标不存在\n");
         printf("发送bbbbb\n");  
         send(netfd,"bbbbb",5,0);
         return -1;
     }
     int fd = open(true_file_path,O_RDWR);
 
     if (fd == -1) {
         printf("open file is failed!\n");
         printf("发送bbbbb\n");  
         send(netfd,"bbbbb",5,0);
         return -1;
     }
     ///////////////////////////////////////////////////////////////////////////正式传输文件
     printf("发送aaaaa成功\n");  
     send(netfd,"aaaaa",5,0);
     train_t *pTrain =(train_t *) calloc(1, sizeof(train_t));
     struct stat statbuf;
     fstat(fd, &statbuf);
     off_t filesize = statbuf.st_size;
     printf("filesize = %ld\n", filesize);
 
     pTrain->length =strlen(file_name);
     memcpy(pTrain->data, file_name, strlen(file_name));
     send(netfd, &pTrain->length, sizeof(pTrain->length), MSG_NOSIGNAL);
     send(netfd, pTrain->data, pTrain->length, MSG_NOSIGNAL);                                   
 
     send(netfd, &filesize,sizeof(off_t), MSG_NOSIGNAL);
 
     while(1) {
         bzero(pTrain->data, sizeof(pTrain->data));
         ssize_t sret =read(fd, pTrain->data, sizeof(pTrain->data));
         if(sret == 0) break;
         pTrain->length = sret;
         send(netfd, &pTrain->length, sizeof(pTrain->length), MSG_NOSIGNAL);
         send(netfd, pTrain->data, pTrain->length, MSG_NOSIGNAL);
 
     }
     printf("传输成功\n");                                                  
     close(fd);
 
     return 0;
 }//func_puts结束
